package com.geeklei.media.ui.widget;

import android.annotation.TargetApi;
import android.os.Build;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geeklei.media.ui.IVideoLifeCycle;
import com.geeklei.media.ui.R;
import com.geeklei.media.ui.common.ErrorEvent;
import com.geeklei.videoplayer.GeekleiVideoView;

import tv.danmaku.ijk.media.exo.IjkExoMediaPlayer;

public class LoadingCycle implements IVideoLifeCycle {
    private static final String TAG = "LoadingCycle";
    private TextView mLoadingText;
    private TextView mLoadingSpeedText;
    private ImageView mLoadingImg;
    private View mLoadingView;
    private RelativeLayout mParent;
    private Animation hyperspaceJumpAnimation;

    public LoadingCycle(RelativeLayout relativeLayout) {
        this.mParent = relativeLayout;
    }

    @TargetApi(Build.VERSION_CODES.M)
    public View getLoadingView() {
        if (this.mLoadingView == null) {
            this.mLoadingView = View.inflate(this.mParent.getContext(), R.layout.vp_loading_view, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            this.mLoadingView.setLayoutParams(params);
            this.mParent.addView(this.mLoadingView);
            this.mLoadingView.setBackgroundColor(Integer.MIN_VALUE);
            this.mLoadingText = (TextView) this.mParent.findViewById(R.id.loading_textview);
            this.mLoadingSpeedText = (TextView) this.mParent.findViewById(R.id.loading_speed_tv);
            this.mLoadingImg = (ImageView) this.mParent.findViewById(R.id.imageView);
            hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this.mParent.getContext(), R.anim.loading_animation); // 加载动画
        }

        this.mLoadingSpeedText.setVisibility(View.VISIBLE);
        this.mLoadingImg.startAnimation(hyperspaceJumpAnimation);             // 使用ImageView显示动画
        this.mLoadingImg.setVisibility(View.VISIBLE);
        this.mLoadingView.bringToFront();
        return this.mLoadingView;
    }

    public void onCompletion(GeekleiVideoView iVideoView) {
    }

    public void onPrepared(GeekleiVideoView iVideoView) {
        hideLoading();
    }

    public void onBufferingStart(GeekleiVideoView iVideoView) {
//        if (iVideoView.canBuffering()) {
        showBuffering();
//        }
    }

    public void onBufferingEnd(GeekleiVideoView iVideoView) {
        hideLoading();
    }

    public void onVideoLoadingStart(GeekleiVideoView iVideoView) {
        if (iVideoView != null) {
            showLoading();
        }
    }

    public void onEpLoadingStart() {
        showLoading();
    }

    public void onBufferingPercent(GeekleiVideoView iVideoView, int percent) {
        if (this.mLoadingText != null) {
            this.mLoadingText.setOnClickListener(null);
            this.mLoadingText.setVisibility(View.VISIBLE);
            this.mLoadingText.setText(this.mLoadingText.getResources().getString(R.string.buffering_video_netplay_label_v5) + "(" + percent + "%)");
        }
    }

    public void onAdsPlayStart(GeekleiVideoView iVideoView) {
    }

    public void onAdsPlayEnd(GeekleiVideoView iVideoView) {
    }

    private void showLoading() {
        getLoadingView().setVisibility(View.VISIBLE);
        this.mLoadingImg.setVisibility(View.VISIBLE);
        if (((double) this.mParent.getWidth()) < 0.5d * ((double) this.mParent.getResources().getDisplayMetrics().widthPixels)) {
            this.mLoadingText.setVisibility(View.GONE);
            return;
        }
    }

    private void showBuffering() {
        getLoadingView().setVisibility(View.VISIBLE);
        this.mLoadingImg.setVisibility(View.VISIBLE);
        if (((double) this.mParent.getWidth()) < 0.5d * ((double) this.mParent.getResources().getDisplayMetrics().widthPixels)) {
            this.mLoadingText.setVisibility(View.GONE);
        } else {
            this.mLoadingText.setOnClickListener(null);
            this.mLoadingText.setVisibility(View.VISIBLE);
            this.mLoadingText.setText(R.string.buffering_video_netplay_label_v5);
        }
    }

    private void hideLoading() {
        if (this.mLoadingView != null) {
            getLoadingView().setVisibility(View.GONE);
        }
    }

    @Override
    public void onInfoListener(GeekleiVideoView iVideoView, int what, int extras) {
        if (what == IjkExoMediaPlayer.MEDIA_INFO_NETWORK_BANDWIDTH
                && mLoadingSpeedText != null
                && mLoadingSpeedText.getVisibility() == View.VISIBLE) {
            Log.d("bandwidth", String.valueOf(extras));
            mLoadingSpeedText.setText(String.valueOf(extras) + " KB/s");
        }
    }

    @Override
    public void onError(GeekleiVideoView iVideoView, int what, int extras, ErrorEvent event) {
        getLoadingView().setVisibility(View.VISIBLE);
        if (this.mLoadingText != null) {
            this.mLoadingImg.setVisibility(View.GONE);
            this.mLoadingText.setVisibility(View.VISIBLE);
            this.mLoadingSpeedText.setVisibility(View.INVISIBLE);
            if (event != null) {
                this.mLoadingText.setText(Html.fromHtml(event.getErrorText() + ""));
                this.mLoadingText.setOnClickListener(event.getOnClickListener());
            }
        }
    }

}
